<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

class AdminUser extends Model
{
    protected $autoWriteTimestamp = true;

    public function role()
    {
        return $this->hasManyThrough('AdminRoles', 'AdminRole', 'admin_id', 'id', 'id', 'role_id');
    }

    public function roles()
    {
        return $this->hasMany('AdminRole', 'admin_id', 'id');
    }

    public function searchUsernameAttr($query, $value)
    {
        if ($value) {
            $query->where('username', 'like', "%{$value}%");
        }
    }

    public function searchNicknameAttr($query, $value)
    {
        if ($value) {
            $query->where('nickname', 'like', "%{$value}%");
        }
    }

    public function searchStatusAttr($query, $value)
    {
        if ($value) {
            $query->where('status', '=', $value);
        }
    }

    public function searchIdAttr($query, $value)
    {
        if ($value) {
            $query->where('id', '=', $value);
        }
    }

    public function getAvatarAttr($value, $data)
    {
        if (empty($value)) {
            return conf('site_domain') . '/static/common/images/noavatar.svg';
        }
        return $value;
    }


}
